/*
 练习三种数据返回的形式
*/
#include <iostream>
using namespace std;
string version1(string &str1, string str2); // 返回的是拷贝
string &version2(string &str1, string str2); // 有副作用
string &version3(string &str1, string str2); // 有问题的实现，返回了局部变量的引用

int main_strquote()
{
    string input;
    string result;
    string copy; // 因为version2有副作用，所以这里需要存一下输入的值
    //输入字符串，存入input
    cout << "Please input the origin string:" << endl;
    getline(cin, input);
    cout << "The inputed string is: " << input << endl;
    copy = input;
    result = version1(input, "###");
    cout << "Your string enhanced: " << result << endl;
    cout << "Your original string: " << input << endl;

    result = version2(input, "$$$");
    cout << "Your string enhanced: " << result << endl;
    cout << "Your original string: " << input << endl;
    cout << "Resetting original string.\n";
    input = copy;
    //这里没有crash，但是比较危险，行为是undefine
    result = version3(input, "@@@");
    cout << "Your string enhanced: " << result << endl;
    cout << "Your original string: " << input << endl;
    /*输出的结果是:
        Please input the origin string:
        helloworld
        The inputed string is: helloworld
        Your string enhanced: ###helloworld###
        Your original string: helloworld
        Your string enhanced: $$$helloworld$$$
        Your original string: $$$helloworld$$$
        Resetting original string.
        Your string enhanced: @@@helloworld@@@
        Your original string: helloworld
    */
    return 0;
}

string version1(string &str1, string str2) {
    string result;
    result = str2 + str1 + str2;
    return result;
}

string &version2(string &str1, string str2) {
    str1 = str2 + str1 + str2;
    return str1;
}

string &version3(string &str1, string str2) {
    string result;
    result = str2 + str1 + str2;
    return result;
}

